﻿
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Camc.OutsourceProcession.Customer.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Authorization;

namespace Camc.OutsourceProcession.Customer
{
	[AbpAuthorize]
	public class ContactAppService : OutsourceProcessionAppServiceBase, IContactAppService
	{
		private readonly IRepository<Contact> _contactRepository;

		public ContactAppService(IRepository<Contact> contactRepository)
		{
			_contactRepository = contactRepository;
		}

		public virtual int CreateOrUpdateContact(ContactDto contactDto)
		{
			return _contactRepository.InsertOrUpdateAndGetId(ObjectMapper.Map<Contact>(contactDto));
		}

		public virtual void DeleteContact(int contactId)
		{
			 _contactRepository.DeleteAsync(contactId);
		}

	
		public virtual List<ContactDto> GetAllContacts(string filterString)
		{
			var filterResult = _contactRepository.GetAll().WhereIf(!filterString.IsNullOrWhiteSpace(),
				c => c.Address.Contains(filterString) ||
				c.Contacts.Contains(filterString) ||
				c.CompanyName.Contains(filterString) ||
				c.Telephone.Contains(filterString)
				).ToList();
			return ObjectMapper.Map<List<ContactDto>>(filterResult);
		}
	}
}
